home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Throwable.java < prev    next >
Text File  |  1998-09-22  |  5KB  |  178 lines

  1. /*
  2.  * @(#)Throwable.java    1.33 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The <code>Throwable</code> class is the superclass of all errors 
  19.  * and exceptions in the Java language. Only objects that are 
  20.  * instances of this class (or of one of its subclasses) are thrown 
  21.  * by the Java Virtual Machine or can be thrown by the Java 
  22.  * <code>throw</code> statement. Similarly, only this class or one of 
  23.  * its subclasses can be the argument type in a <code>catch</code> 
  24.  * clause. 
  25.  * <p>
  26.  * A <code>Throwable</code> class contains a snapshot of the 
  27.  * execution stack of its thread at the time it was created. It can 
  28.  * also contain a message string that gives more information about 
  29.  * the error. 
  30.  * <p>
  31.  * Here is one example of catching an exception: 
  32.  * <p><blockquote><pre>
  33.  *     try {
  34.  *         int a[] = new int[2];
  35.  *         a[4];
  36.  *     } catch (ArrayIndexOutOfBoundsException e) {
  37.  *         System.out.println("exception: " + e.getMessage());
  38.  *         e.printStackTrace();
  39.  *     }
  40.  * </pre></blockquote>
  41.  *
  42.  * @author  unascribed
  43.  * @version 1.31, 01/26/97
  44.  * @since   JDK1.0
  45.  */
  46. public class Throwable implements java.io.Serializable {
  47.     /**
  48.      * Native code saves some indication of the stack backtrace in this
  49.      * slot.
  50.      */
  51.     private transient Object backtrace;    
  52.  
  53.     /**
  54.      * Specific details about the Throwable.  For example,
  55.      * for FileNotFoundThrowables, this contains the name of
  56.      * the file that could not be found.
  57.      */
  58.     private String detailMessage;
  59.  
  60.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  61.     private static final long serialVersionUID = -3042686055658047285L;
  62.  
  63.     /**
  64.      * Constructs a new <code>Throwable</code> with no detail message. 
  65.      * The stack trace is automatically filled in. 
  66.      *
  67.      * @since   JDK1.0
  68.      */
  69.     public Throwable() {
  70.     fillInStackTrace();
  71.     }
  72.  
  73.     /**
  74.      * Constructs a new <code>Throwable</code> with the specified detail 
  75.      * message. The stack trace is automatically filled in. 
  76.      *
  77.      * @param   message   the detail message.
  78.      * @since   JDK1.0
  79.      */
  80.     public Throwable(String message) {
  81.     fillInStackTrace();
  82.     detailMessage = message;
  83.     }
  84.  
  85.     /**
  86.      * Returns the detail message of this throwable object.
  87.      *
  88.      * @return  the detail message of this <code>Throwable</code>,
  89.      *          or <code>null</code> if this <code>Throwable</code> does not
  90.      *          have a detail message.
  91.      * @since   JDK1.0
  92.      */
  93.     public String getMessage() {
  94.     return detailMessage;
  95.     }
  96.  
  97.     /**
  98.      * Creates a localized description of this <code>Throwable</code>.
  99.      * Subclasses may override this method in order to produce a
  100.      * locale-specific message.  For subclasses that do not override this
  101.      * method, the default implementation returns the same result as
  102.      * <code>getMessage()</code>.
  103.      *
  104.      * @since   JDK1.1
  105.      */
  106.     public String getLocalizedMessage() {
  107.     return getMessage();
  108.     }
  109.  
  110.     /**
  111.      * Returns a short description of this throwable object.
  112.      *
  113.      * @return  a string representation of this <code>Throwable</code>.
  114.      * @since   JDK1.0
  115.      */
  116.     public String toString() {
  117.     String s = getClass().getName();
  118.     String message = getMessage();
  119.     return (message != null) ? (s + ": " + message) : s;
  120.     }
  121.  
  122.     /**
  123.      * Prints this <code>Throwable</code> and its backtrace to the 
  124.      * standard error stream. 
  125.      *
  126.      * @see     java.lang.System#err
  127.      * @since   JDK1.0
  128.      */
  129.     public void printStackTrace() { 
  130.         System.err.println(this);
  131.     printStackTrace0(System.err);
  132.     }
  133.  
  134.     /**
  135.      * Prints this <code>Throwable</code> and its backtrace to the 
  136.      * specified print stream. 
  137.      *
  138.      * @since   JDK1.0
  139.      */
  140.     public void printStackTrace(java.io.PrintStream s) { 
  141.         s.println(this);
  142.     printStackTrace0(s);
  143.     }
  144.  
  145.     /**
  146.      * Prints this <code>Throwable</code> and its backtrace to the specified
  147.      * print writer.
  148.      *
  149.      * @since   JDK1.1
  150.      */
  151.     public void printStackTrace(java.io.PrintWriter s) { 
  152.         s.println(this);
  153.     printStackTrace0(s);
  154.     }
  155.  
  156.     /* The given object must have a void println(char[]) method */
  157.     private native void printStackTrace0(Object s);
  158.  
  159.     /**
  160.      * Fills in the execution stack trace. This method is useful when an 
  161.      * application is re-throwing an error or exception. For example: 
  162.      * <p><blockquote><pre>
  163.      *     try {
  164.      *         a = b / c;
  165.      *     } catch(ArithmeticThrowable e) {
  166.      *         a = Number.MAX_VALUE;
  167.      *         throw e.fillInStackTrace();
  168.      *     }
  169.      * </pre></blockquote>
  170.      *
  171.      * @return  this <code>Throwable</code> object.
  172.      * @see     java.lang.Throwable#printStackTrace()
  173.      * @since   JDK1.0
  174.      */
  175.     public native Throwable fillInStackTrace();
  176.  
  177. }
  178.